import log4js from 'log4js';
import path from 'path';
import util from 'util';
const LOG_PATH = path.join('', 'logs')

log4js.configure({
    pm2: true,
    pm2InstanceVar: "INSTANCE_ID",
    appenders: {
        logFile: {
            type: 'dateFile',
            filename: path.join(LOG_PATH, "main"),
            pattern: 'yyyy-MM-dd.log',
            keepFileExt: true,
            alwaysIncludePattern: true,
            layout: {
                type: 'pattern',
                pattern: '%m%n'
            }
        },
        out: {
            type: 'console'
        }
    },
    categories: {
        default: {
            appenders: ['out'],
            level: 'INFO'
        },
        main: {
            appenders: ['out', 'logFile'],
            level: 'DEBUG'
        }
    }
})
const logger = log4js.getLogger("main")
//重写console.log
console.log = function () {
    logger.info('%c' + new Date().toLocaleString() + ' log' + '\n' + util.format.apply(null, arguments), 'color: green;');
}
console.info = function () {
    // logger.info(util.format.apply(null, arguments));
    logger.info('%c' + new Date().toLocaleString() + 'info' + '\n' + util.format.apply(null, arguments), 'color: green;');
}
console.error = function () {
    logger.error('%c' + new Date().toLocaleString() + 'error' + '\n' + util.format.apply(null, arguments), 'color: red;');

}

export default {
    main: log4js.getLogger("main")
}
